from dice import *
import plotly.express as px

# 创建⼀个 D6
# dice = Dice()
# 创建两个D6
# dice_1 = Dice()
# dice_2 = Dice()
# 创建⼀个D6和⼀个D10
dice_1 = Dice()
dice_2 = Dice(10)

# 掷⼏次骰⼦并将结果存储在⼀个列表中
# results = []
#
# # for roll_num in range(1000):
# for roll_num in range(5000):
#     # result = dice.roll()
#     result = dice_1.roll() + dice_2.roll()
#     results.append(result)
# print(results)

# 列表推导式代替for循环
results = [(dice_1.roll() + dice_2.roll()) for roll_num in range(5000)]

# 分析结果
frequencies = []
# poss_results = range(1, dice.num_sides+1)
max_result = dice_1.num_sides + dice_2.num_sides
poss_results = range(2, max_result+1)
# print(poss_results)

for value in poss_results:
    frequency = results.count(value)
    frequencies.append(frequency)
# print(frequencies)

# 对结果进⾏可视化
# fig = px.bar(x=poss_results, y=frequencies)
# fig = px.scatter(x=poss_results, y=frequencies)
# fig = px.line(x=poss_results, y=frequencies)

# title = "Results of Rolling One D6 1,000 Times"
# title = "Results of Rolling Two D6 Dice 1,000 Times"
title = "Results of Rolling a D6 and a D10 50,000 Times"
labels = {'x': 'Result', 'y': 'Frequency of Result'}
fig = px.bar(x=poss_results, y=frequencies, title=title,
             labels=labels)

# 进⼀步定制图形
fig.update_layout(xaxis_dtick=1)

fig.show()
# fig.write_html('dice_visual_d6d10.html')